package o;

import android.content.Context;
import android.os.Build.VERSION;
import android.text.TextUtils;
import com.huawei.hms.support.hianalytics.HiAnalyticsConstant;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.security.cert.CertificateEncodingException;
import java.security.cert.X509Certificate;
import java.util.UUID;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

public class fco {
    private static ScheduledExecutorService ʽ = Executors.newSingleThreadScheduledExecutor();
    private static int[] ˊ = new int[]{1};
    private static boolean ˋ = false;
    private static final Object ˎ = new Object();
    private static String ˏ = null;
    private static String ॱ = null;
    private static int ॱॱ = 0;

    public interface c {
        void ˋ(String str);
    }

    static class d implements Runnable {
        private c ˋ;

        public d(c cVar) {
            this.ˋ = cVar;
        }

        public void run() {
            synchronized (fco.ˎ) {
                if (!fco.ˋ) {
                    Logger.ˏ("HUKSUtil", "start getCertificateChain");
                    if (etp.ˋ()) {
                        Logger.e("HUKSUtil", "root device, getCertificateChain fail.");
                        return;
                    } else if (fco.ᐝ()) {
                        try {
                            X509Certificate[] ˏ = fco.ॱ(ContentManager.getInstant().getContext(), fco.ॱ.getBytes("UTF-8"));
                            if (ˏ == null || ˏ.length != 4) {
                                Logger.e("HUKSUtil", ˏ == null ? "X509Certificate is null!" : "X509Certificate length error:" + ˏ.length);
                                fco.ॱ();
                                if (fco.ॱॱ >= 3) {
                                    Logger.ˋ("HUKSUtil", "getCertificateChain retry count max!");
                                    fco.ˋ = true;
                                }
                            } else {
                                StringBuilder stringBuilder = new StringBuilder();
                                for (int i = 0; i < 3; i++) {
                                    stringBuilder.append(dnk.ˏ(ˏ[i].getEncoded())).append(HiAnalyticsConstant.REPORT_VAL_SEPARATOR);
                                }
                                stringBuilder.append(fco.ॱ);
                                fco.ˏ = stringBuilder.toString();
                                fco.ˋ = true;
                                fco.ॱ();
                                if (fco.ॱॱ >= 3) {
                                    Logger.ˋ("HUKSUtil", "getCertificateChain retry count max!");
                                    fco.ˋ = true;
                                }
                            }
                        } catch (UnsupportedEncodingException e) {
                            Logger.e("HUKSUtil", "getCertificateChain UnsupportedEncodingException");
                        } catch (CertificateEncodingException e2) {
                            Logger.e("HUKSUtil", "certificates CertificateEncodingException");
                        }
                    } else {
                        Logger.e("HUKSUtil", "no phone permission, getCertificateChain fail.");
                        return;
                    }
                }
                fco.ˎ(this.ˋ);
            }
        }
    }

    static /* synthetic */ int ॱ() {
        int i = ॱॱ;
        ॱॱ = i + 1;
        return i;
    }

    public static synchronized void ˋ(c cVar) {
        synchronized (fco.class) {
            if (!TextUtils.isEmpty(ˏ) || ˋ) {
                ˎ(cVar);
            } else {
                if (TextUtils.isEmpty(ॱ)) {
                    ॱ = UUID.randomUUID().toString().replace("-", "");
                }
                ʽ.execute(new d(cVar));
            }
        }
    }

    private static void ˎ(c cVar) {
        Logger.ˏ("HUKSUtil", "getCertificateChainStr result isEmpty:" + TextUtils.isEmpty(ˏ));
        if (cVar != null) {
            cVar.ˋ(ˏ);
        }
    }

    private static X509Certificate[] ॱ(Context context, byte[] bArr) {
        try {
            return (X509Certificate[]) Class.forName("com.huawei.security.keystore.HwAttestationUtils").getMethod("attestDeviceIds", new Class[]{Context.class, int[].class, byte[].class}).invoke(null, new Object[]{context, ˊ, bArr});
        } catch (ClassNotFoundException e) {
            Logger.e("HUKSUtil", "getCertificateChain ClassNotFoundException");
            return null;
        } catch (NoSuchMethodException e2) {
            Logger.e("HUKSUtil", "getCertificateChain NoSuchMethodException");
            return null;
        } catch (IllegalAccessException e3) {
            Logger.e("HUKSUtil", "getCertificateChain IllegalAccessException");
            return null;
        } catch (InvocationTargetException e4) {
            Logger.e("HUKSUtil", "getCertificateChain InvocationTargetException");
            return null;
        } catch (Exception e5) {
            Logger.e("HUKSUtil", "getCertificateChain Exception");
            return null;
        }
    }

    private static boolean ᐝ() {
        if (VERSION.SDK_INT >= 23 && ContentManager.getInstant().getContext().checkSelfPermission("android.permission.READ_PHONE_STATE") != 0) {
            return false;
        }
        return true;
    }
}
